import { getdgameApi } from "../utils/api.js";

const detail = {
  state() {
    return {
      gamelist: {},
    };
  },
  mutations: {
    //将获取的数据传入返回值
    getGameList(state, payload) {
      state.gamelist = payload;
    },

    //删除游戏
    removeGame(state, payload) {
      //获取本地存储
      let arr = JSON.parse(localStorage.getItem("id"));
      //将传输过来的id对应的数组项删除
      arr.splice(arr.indexOf(payload.id), 1);
      if (arr.length === 0) {
        localStorage.removeItem("id");
      } else {
        localStorage.setItem("id", JSON.stringify(arr));
      }
      //过滤掉gamelis中存在传入id的数组项
      state.gamelist = state.gamelist.filter((item) => {
        return item.id !== payload.id;
      });
    },
  },
  actions: {
    //获取详情页接口的返回数据
    getGameList({ commit }, payload) {
      return new Promise((resolve) => {
        getdgameApi(payload).then((res) => {
          commit("getGameList", res.result);
          resolve();
        });
      });
    },
  },
};

export default detail;
